package com.hsteach.web.controller.visit;

import com.google.common.collect.Maps;
import com.hsteach.common.annotation.Log;
import com.hsteach.common.core.controller.BaseController;
import com.hsteach.common.core.domain.AjaxResult;
import com.hsteach.common.core.domain.R;
import com.hsteach.common.core.page.TableDataInfo;
import com.hsteach.common.enums.BusinessType;
import com.hsteach.common.enums.OperatorType;
import com.hsteach.common.utils.SecurityUtils;
import com.hsteach.dao.domain.visit.VisitPeers;
import com.hsteach.dao.domain.visit.VisitReservation;
import com.hsteach.framework.feign.client.xgk.dto.ret.GetTeacherRet;
import com.hsteach.service.third.XgkService;
import com.hsteach.service.visit.VisitPeersServiceImpl;
import com.hsteach.service.visit.VisitRecordService;
import com.hsteach.service.visit.VisitReservationService;
import com.hsteach.web.controller.visit.dto.*;
import com.hsteach.web.controller.visit.vo.ListRecordVo;
import com.hsteach.web.controller.visit.vo.ListReservationVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 家访预约Controller
 * 
 * @author fanxquer
 * @date 2023-07-21
 */
@Api(value = "家访-家访操作", tags = "家访-家访操作")
@RestController
@RequestMapping("/visit")
public class VisitRecordController extends BaseController
{
    @Autowired
    private VisitReservationService visitReservationService;

    @Autowired
    private VisitRecordService visitRecordService;

    @Autowired
    private VisitPeersServiceImpl visitPeersService;

    @Autowired
    private XgkService xgkService;

    /**
     * 查询家访预约列表
     */
    @ApiOperation("查询家访预约列表，date为1查当天，为2查一周")
    @GetMapping("/reservation/list")
    public R<List<ListReservationVo>> list(@RequestParam("date") Integer date)
    {
        List<ListReservationVo> list = visitReservationService.selectLatestReservation(date);
        return R.ok(list);
    }

    @ApiOperation("添加家访记录")
    @Log(title = "添加家访记录", businessType = BusinessType.INSERT, operatorType = OperatorType.MOBILE)
    @PostMapping
    public AjaxResult add(@RequestBody AddVisitRecordDto dto)
    {
        visitRecordService.addVisitRecord(dto);
        return AjaxResult.success();
    }

    @ApiOperation("初始化家访记录,没有预约直接进行填写的情况下先调用生成recordId")
    @Log(title = "初始化家访记录", businessType = BusinessType.INSERT, operatorType = OperatorType.MOBILE)
    @PostMapping("/init/{personId}")
    public AjaxResult initRecord(@PathVariable("personId") String personId)
    {
        Long aLong = visitRecordService.initRecord(personId);
        Map<String, Object> data = Maps.newHashMap();
        data.put("id", aLong);
        return AjaxResult.success(data);
    }

    @ApiOperation("查询家访记录列表")
    @PostMapping("/list")
    public TableDataInfo<ListRecordVo> listRecord(@RequestBody ListRecordDto dto)
    {
        return visitRecordService.listRecord(dto);
    }

    @ApiOperation("查询家访记录详情")
    @GetMapping("/{id}")
    public R<GetVisitRecordDto> getRecordInfo(@PathVariable("id") Long id)
    {
        return R.ok(visitRecordService.getRecordInfo(id));
    }

    /**
     * 删除家访记录
     */
    @ApiOperation("删除家访记录")
    @Log(title = "删除家访记录", businessType = BusinessType.DELETE, operatorType = OperatorType.MOBILE)
    @DeleteMapping("/{id}")
    public AjaxResult removeRecordInfo(@PathVariable Long id)
    {
        return toAjax(visitRecordService.deleteVisitRecordById(id));
    }

    /**
     * 新增家访同行人
     */
    @ApiOperation("新增家访同行人，列表同通讯录接口")
    @Log(title = "新增家访同行人", businessType = BusinessType.INSERT, operatorType = OperatorType.MOBILE)
    @PostMapping("/peer")
    public AjaxResult add(@RequestBody AddVisitPeerDto dto)
    {
        visitPeersService.addVisitPeer(dto);
        return AjaxResult.success();
    }

    /**
     * 删除家访同行人
     */
    @ApiOperation("删除家访同行人")
    @Log(title = "删除家访同行人", businessType = BusinessType.DELETE, operatorType = OperatorType.MOBILE)
    @DeleteMapping("/peer/{id}")
    public AjaxResult remove(@PathVariable("id") Long id)
    {
        return toAjax(visitPeersService.deleteVisitPeersByIds(new Long[]{id}));
    }

    /**
     * 查询家访同行人列表
     */
    @ApiOperation("查询家访同行人列表")
    @GetMapping("/peer/{recordId}")
    public R<List<VisitPeers>> listPeers(@PathVariable("recordId") Long recordId)
    {
        VisitPeers query = new VisitPeers();
        query.setRecordId(recordId);
        List<GetTeacherRet> teachers = xgkService.getTeachers(SecurityUtils.getAuthInfo().getOrgCode());
        Map<String, GetTeacherRet> jobMap = teachers.stream().collect(Collectors.toMap(GetTeacherRet::getPersonId, e -> e, (v1, v2) -> v2));
        List<VisitPeers> list = visitPeersService.selectVisitPeersList(query);
        list.stream().forEach(e -> {
            if (jobMap.containsKey(e.getPeerPersonId())) {
                GetTeacherRet teacherRet = jobMap.get(e.getPeerPersonId());
                e.setJobList(teacherRet.getJobList());
            }
        });
        return R.ok(list);
    }

    /**
     * 新增家访预约
     */
    @ApiOperation("新增家访预约")
    @Log(title = "新增家访预约", businessType = BusinessType.INSERT, operatorType = OperatorType.MOBILE)
    @PostMapping("/reservation")
    public AjaxResult addReservation(@RequestBody AddReservationDto dto)
    {
        return toAjax(visitReservationService.addReservation(dto));
    }

    /**
     * 删除家访预约
     */
    @ApiOperation("删除家访预约")
    @Log(title = "删除家访预约", businessType = BusinessType.DELETE, operatorType = OperatorType.MOBILE)
    @DeleteMapping("/reservation/{id}")
    public AjaxResult removeReservation(@PathVariable Long id)
    {
        return toAjax(visitReservationService.deleteVisitReservationById(id));
    }

    /**
     * 修改家访预约时间
     */
    @ApiOperation("修改家访预约时间")
    @Log(title = "修改家访预约时间", businessType = BusinessType.UPDATE, operatorType = OperatorType.MOBILE)
    @PutMapping("/reservation")
    public AjaxResult edit(@RequestBody UpdateReservationDto dto)
    {
        VisitReservation visitReservation = new VisitReservation();
        visitReservation.setId(dto.getReservationId());
        visitReservation.setBeginTime(dto.getBeginTime());
        visitReservation.setEndTime(dto.getEndTime());
        return toAjax(visitReservationService.updateVisitReservation(visitReservation));
    }

    /**
     * 家长端 查询家访预约列表
     * type 0发起  1 已完成  2 已取消
     */
    @ApiOperation("查询家访预约列表")
    @GetMapping("/reservation/getList")
    public R getList(ReservationListDto dto)
    {
        return R.ok(visitReservationService.getList(dto));
    }


    /**
     * 取消预约
     */
    @ApiOperation("取消家访预约")
    @GetMapping("/reservation/cancel")
    public AjaxResult cancel(@Valid ReservationDto dto)
    {
        return toAjax(visitReservationService.cancel(dto));
    }

    /**
     * 家长端 已知晓
     */
    @ApiOperation("已知晓")
    @GetMapping("/reservation/pass")
    public AjaxResult pass(Long id)
    {
        return toAjax(visitReservationService.pass(id));
    }

}
